<?php

class RegistrationModel extends dmq\Model
{
	public function register( $username, $password, $email )
	{
		$password = sha1( $password );

		// Check if username is taken.
		$stmt = $this->db->prepare( 'SELECT userId FROM Users WHERE name = ?' );
		$stmt->bind_param( 's', $username );
		$stmt->execute();
		$stmt->bind_result( $row_id );
		$stmt->fetch();
		$nameTaken = !!$row_id;
		$stmt->close();

		if ( $nameTaken )
		{
			throw new Exception( 'The usename is already taken.' );
		}

		// Insert user into database.
		$stmt = $this->db->prepare( 'INSERT INTO Users ( name, password, email ) VALUES( ?, UNHEX( ? ), ? )' );
		$stmt->bind_param( 'sss', $username, $password, $email );
		$stmt->execute();
		$stmt->close();
	}
}
